/* $Header: valve_tbls.c,v 2.2 01/10/02 10:29:25 przybyls Exp $ */

/* valve_tbls.c - workfile get/set for keyboard valves */
/* Bob Cook; Oct 1993 */
/* Copyright (c) 1993, Hewlett-Packard Co. */




#include  <typedefs.h>
#include  <pcb_defs.h>
#include  <error.h>
#include  <stddef.h>
#include  <method.h>
#include  <zone_config.h>
#include  <meth_config.h>
#include <sequence.h>
#include  <seq_ui.h>

#include <valve_ui.h>


/* definitions used with DummyFunction */
typedef UI_ERR (* const SET)(void *status, INT32 setpt, U_INT8 unused_parm);
typedef INT32  (* const GET)(void *status, U_INT8 unused_parm);


/********************* wrapper function prototypes *********************/
/*
 * Format:
 *    Get commands:
 *        mGetWrapperCommand(void *parameter, U_INT8 number)
 *    Set commands:
 *        mSetWrapperCommand(void *parameter, INT32 setpoint, U_INT8 number)
 *    Examples:
 *       parameter - pointer to oven, detector or inlet zones
 *       setpoint - value of setpoint from received command
 *       number - as ramp number
 */

static INT32  mGetValveActual(void *parm, U_INT8 num);
static INT32  mGetValveSetpt(void *parm, U_INT8 num);
static INT32  mGetValveType(void *parm, U_INT8 num);
static INT32  mGetValveLoopVolume(void *parm, U_INT8 num);
static INT32  mGetValveStreamNo(void *parm, U_INT8 num);
static INT32  mGetMultiTime(void *parm, U_INT8 num);
static INT32  mGetInvertBcd(void *parm, U_INT8 num);
static INT32  mGetValveLoadTime(void *parm, U_INT8 num);
static INT32  mGetValveInjectTime(void *parm, U_INT8 num);
static INT32  mGetValveInlet(void *parm, U_INT8 num);
static INT32  mGetGsvInjectionState(void *parm, U_INT8 num);
static INT32  mGetValveBcdInput(void *parm, U_INT8 num);

static UI_ERR  mSetValveState(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveType(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveLoopVolume(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveStreamNo(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetMultiTime(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetInvertBcd(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveLoadTime(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveInjectTime(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mSetValveInlet(void *parm, INT32 setpt, U_INT8 num);

static UI_ERR  mLetValveType(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mLetValveLoopVolume(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mLetValveInlet(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mLetMultiTime(void *parm, INT32 setpt, U_INT8 num);
static UI_ERR  mLetInvertBcd(void *parm, INT32 setpt, U_INT8 num);

static UI_ERR DummyFunction(void *parm);
static UI_ERR SWPreFunction(void *parm);
static UI_ERR GSPreFunction(void *parm);
static UI_ERR MPPreFunction(void *parm);

/*** create constants used by "parameter" which passes the valve number ***/

static const U_INT8  VALVE1 = 1;
static const U_INT8  VALVE2 = 2;
static const U_INT8  VALVE3 = 3;
static const U_INT8  VALVE4 = 4;
static const U_INT8  VALVE5 = 5;
static const U_INT8  VALVE6 = 6;
static const U_INT8  VALVE7 = 7;
static const U_INT8  VALVE8 = 8;


/********************* command table entries *********************/
/*
 * Format:
 *   static const METHCMD_ENTRY mmCommandTableEntries[] =
 *   {
 *     {mGetWrapperCommand, mSetWrapperCommand, number, type of setpoint}
 *   }
 */

static const METHCMD_ENTRY mmVXSW[] =
{
        {mGetValveSetpt, mSetValveState, 0, METH_INT0}
};

static const METHCMD_ENTRY mmVXGS[] =
{
        {mGetValveSetpt, mSetValveState, 0, METH_INT0},
        {mGetValveLoadTime, mSetValveLoadTime, 0, METH_INT2},
        {mGetValveInjectTime, mSetValveInjectTime, 0, METH_INT2}
};

static const METHCMD_ENTRY mmVXMP[] =
{
        {mGetValveStreamNo, mSetValveStreamNo, 0, METH_INT0}
};

static const METHCMD_ENTRY mmVXCF[] =
{
        {mGetValveType, mSetValveType, 0, METH_INT0},
        {mGetValveLoopVolume, mSetValveLoopVolume, 0, METH_INT3},
        {mGetValveInlet, mSetValveInlet, 0, METH_INT0},
        {mGetMultiTime, mSetMultiTime, 0, METH_INT1},
        {mGetInvertBcd, mSetInvertBcd, 0, METH_INT0}
};

static const METHCMD_ENTRY mmVXCW[] =
{
        {mGetValveType, mLetValveType, 0, METH_INT0},
        {mGetValveLoopVolume, mLetValveLoopVolume, 0, METH_INT3},
        {mGetValveInlet, mLetValveInlet, 0, METH_INT0},
        {mGetMultiTime, mLetMultiTime, 0, METH_INT1},
        {mGetInvertBcd, mLetInvertBcd, 0, METH_INT0}
};

static const METHCMD_ENTRY mmVXST[] =
{
        {mGetValveActual,       (SET)DummyFunction, 1, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 2, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 3, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 4, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 5, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 6, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 7, METH_INT0},
        {mGetValveActual,       (SET)DummyFunction, 8, METH_INT0},
        {mGetGsvInjectionState, (SET)DummyFunction, 0, METH_INT0},
        {mGetValveBcdInput,     (SET)DummyFunction, 0, METH_INT0}
};

/********************* command tables *********************/
/*
 * Need on for each command.  If there are multible locations
 *    such as front/back or V1/V2/V3...V8 need one command table
 *    for each.  "paramter" is used to indicate which location.
 * Format:
 *   const METHCMD mCommandTable[] =
 *   {
 *      MAXINDEX(mmCommandTableEntries, METHCMD_ENTRY),
 *      &mmCommandTableEntries[starting_index_in_table],
 *      parameter,    --- need a pointer to a const = (void * const)
 *      PreCommandFunction,
 *      PostCommandFunction
 *   }
 */

const METHCMD mV1SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE1,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV2SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE2,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV3SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE3,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV4SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE4,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV5SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE5,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV6SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE6,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV7SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE7,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV8SW =
{
        MAXINDEX(mmVXSW, METHCMD_ENTRY),
        &mmVXSW[0],
        (U_INT8 * const)&VALVE8,
        SWPreFunction,
        DummyFunction
};

const METHCMD mV1GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE1,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV2GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE2,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV3GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE3,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV4GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE4,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV5GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE5,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV6GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE6,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV7GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE7,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV8GS =
{
        MAXINDEX(mmVXGS, METHCMD_ENTRY),
        &mmVXGS[0],
        (U_INT8 * const)&VALVE8,
        GSPreFunction,
        DummyFunction
};

const METHCMD mV1MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE1,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV2MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE2,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV3MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE3,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV4MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE4,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV5MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE5,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV6MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE6,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV7MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE7,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV8MP =
{
        MAXINDEX(mmVXMP, METHCMD_ENTRY),
        &mmVXMP[0],
        (U_INT8 * const)&VALVE8,
        MPPreFunction,
        DummyFunction
};

const METHCMD mV1CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE1,
        DummyFunction,
        DummyFunction
};

const METHCMD mV2CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE2,
        DummyFunction,
        DummyFunction
};

const METHCMD mV3CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE3,
        DummyFunction,
        DummyFunction
};

const METHCMD mV4CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE4,
        DummyFunction,
        DummyFunction
};

const METHCMD mV5CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE5,
        DummyFunction,
        DummyFunction
};

const METHCMD mV6CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE6,
        DummyFunction,
        DummyFunction
};

const METHCMD mV7CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE7,
        DummyFunction,
        DummyFunction
};

const METHCMD mV8CF =
{
        MAXINDEX(mmVXCF, METHCMD_ENTRY),
        &mmVXCF[0],
        (U_INT8 * const)&VALVE8,
        DummyFunction,
        DummyFunction
};

const METHCMD mV1CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE1,
        DummyFunction,
        DummyFunction
};

const METHCMD mV2CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE2,
        DummyFunction,
        DummyFunction
};

const METHCMD mV3CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE3,
        DummyFunction,
        DummyFunction
};

const METHCMD mV4CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE4,
        DummyFunction,
        DummyFunction
};

const METHCMD mV5CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE5,
        DummyFunction,
        DummyFunction
};

const METHCMD mV6CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE6,
        DummyFunction,
        DummyFunction
};

const METHCMD mV7CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE7,
        DummyFunction,
        DummyFunction
};

const METHCMD mV8CW =
{
        MAXINDEX(mmVXCW, METHCMD_ENTRY),
        &mmVXCW[0],
        (U_INT8 * const)&VALVE8,
        DummyFunction,
        DummyFunction
};

const METHCMD mVXST =
{
        MAXINDEX(mmVXST, METHCMD_ENTRY),
        &mmVXST[0],
        (void *)NULL,
        DummyFunction,
        DummyFunction
};


/********************* "get" wrapper functions *********************/

static UI_ERR DummyFunction(void *parm)
{
        (void) parm;
        return OK;
}


static INT32  mGetValveSetpt(void *parm, U_INT8 num)
{
        (void) num;
        if( GetValveSetpt(*(U_INT8 * const)parm) == ON )
        {
           return 1;
        }
        else
        {
           return 0;
        }
}

static INT32  mGetValveActual(void *parm, U_INT8 num)
{
        (void) parm;
        if( GetValveState(num) == ON )
        {
           return 1;
        }
        else
        {
           return 0;
        }
}


static INT32  mGetValveType(void *parm, U_INT8 num)
{
        (void) num;
        return (INT32) GetValveType(*(U_INT8 * const)parm);
}

static INT32  mGetValveLoopVolume(void *parm, U_INT8 num)
{
        (void) num;
	if( GetValveType(*(U_INT8 * const)parm) == GAS_SAMPLING )
	{
	   return GetValveLoopVolume(*(U_INT8 * const)parm);
	}
	else
	{
	   return 0;
	}
}

static INT32  mGetValveStreamNo(void *parm, U_INT8 num)
{
        (void) parm;
        (void) num;
        return GetValveStreamNo();
}

static INT32  mGetMultiTime(void *parm, U_INT8 num)
{
        (void) num;
        if( GetValveType(*(U_INT8 * const)parm) == MULTIPOSITION )
        {
	   return  GetMultiTime();
        }
        else
        {
           return 0;
        }
}

static INT32  mGetInvertBcd(void *parm, U_INT8 num)
{
        (void) parm;
        (void) num;
        
        if( (GetInvertBcd() == ON) &&
                (GetValveType(*(U_INT8 * const)parm) == MULTIPOSITION) )
        {
           return 1;
        }
        else
        {
           return 0;
        }
}

static INT32  mGetValveLoadTime(void *parm, U_INT8 num)
{
        (void) num;
        return GetValveLoadTime(*(U_INT8 * const)parm);
}

static INT32  mGetValveInjectTime(void *parm, U_INT8 num)
{
        (void) num;
        return GetValveInjectTime(*(U_INT8 * const)parm);
}

static INT32  mGetValveInlet(void *parm, U_INT8 num)
{
        (void) num;

        if( GetValveType(*(U_INT8 * const)parm) == GAS_SAMPLING )
        {
	   return (INT32) GetValveInlet(*(U_INT8 * const)parm);
        }
        else
        {
           return 0;
        }
}


static INT32  mGetGsvInjectionState(void *parm, U_INT8 num)
{
        (void) parm;
        (void) num;
        return (INT32) GetGsvInjectionState();
}
static INT32  mGetValveBcdInput(void *parm, U_INT8 num)
{
        (void) parm;
        (void) num;
        return (INT32) GetValveBcdInput();
}

/********************* "set" wrapper functions *********************/

/* sets workfile value */
static UI_ERR  mSetValveState(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   if ((ON_OFF) setpt == ON)
   {
      return SetValveState(ON, *(U_INT8 * const)parm);
   }
   else if ((ON_OFF) setpt == OFF)
   {
      return SetValveState(OFF, *(U_INT8 * const)parm);
   }
   else
   {
      return INVALID_PARAM;
   }
}
static UI_ERR  mSetValveType(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   return  SetValveType(*(U_INT8 * const)parm,(VALVE_TYPE)setpt);
}

static UI_ERR  mSetValveLoopVolume(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   if( GetValveType(*(U_INT8 * const)parm) == GAS_SAMPLING )
   {
     return  SetValveLoopVolume(*(U_INT8 * const)parm,setpt);
   }
   else
   {
     return OK;
   }

}


static UI_ERR  mSetValveStreamNo(void *parm, INT32 setpt, U_INT8 num)
{
   (void) parm;
   (void) num;
   return  SetValveStreamNo(setpt);
}


static UI_ERR  mSetMultiTime(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   if( GetValveType(*(U_INT8 * const)parm) == MULTIPOSITION )
   {
      return  SetMultiTime(setpt);
   }
   else
   {
     return OK;
   }
}

static UI_ERR  mSetInvertBcd(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   if( GetValveType(*(U_INT8 * const)parm) == MULTIPOSITION )
   {
      if( (ON_OFF) setpt == ON )
      {
	 return SetInvertBcd(ON);
      }
      else if( (ON_OFF) setpt == OFF )
      {
	 return SetInvertBcd(OFF);
      }
      else
      {
	 return INVALID_PARAM;
      }
   }
   else
   {
      return OK;
   }
}

static UI_ERR  mSetValveLoadTime(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   return  SetValveLoadTime(*(U_INT8 * const)parm,setpt);
}

static UI_ERR  mSetValveInjectTime(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   return  SetValveInjectTime(*(U_INT8 * const)parm,setpt);
}

static UI_ERR  mSetValveInlet(void *parm, INT32 setpt, U_INT8 num)
{
   (void) num;
   if( GetValveType(*(U_INT8 * const)parm) == GAS_SAMPLING )
   {
      return  SetValveInlet(*(U_INT8 * const)parm,(VALVE_POSITION)setpt);
   }
   else
   {
      return OK;
   }
}

/********************* "let" wrapper functions *********************/

static UI_ERR  mLetValveType(void *parm, INT32 setpt, U_INT8 num)
{
        (void) num;

        gccw.ValveType[*(U_INT8 * const)parm - 1] = (VALVE_TYPE)setpt;
        return OK;
}

static UI_ERR  mLetValveLoopVolume(void *parm, INT32 setpt, U_INT8 num)
{
        (void) num;

        gccw.ValveLoopVolume[*(U_INT8 * const)parm - 1] = setpt;
        return OK;
}

static UI_ERR  mLetValveInlet(void *parm, INT32 setpt, U_INT8 num)
{
        (void) num;

        gccw.ValveInlet[*(U_INT8 * const)parm - 1] = (VALVE_POSITION)setpt;
        return OK;
}

static UI_ERR  mLetMultiTime(void *parm, INT32 setpt, U_INT8 num)
{
        (void) num;

        gccw.MultiTime[*(U_INT8 * const)parm - 1] = setpt;
        return OK;
}

static UI_ERR  mLetInvertBcd(void *parm, INT32 setpt, U_INT8 num)
{
        (void) num;

        gccw.InvertBcd[*(U_INT8 * const)parm - 1] = (ON_OFF)setpt;
        return OK;
}

/******************* "pre" and "post" functions ********************/

static UI_ERR SWPreFunction(void *parm)
{
   if ( (GetValveType(*(U_INT8 * const)parm) == SELECTION) ||
                (GetValveType(*(U_INT8 * const)parm) == OTHER) )
   {
      return OK;
   }
   else
   {
      return NOT_INSTALLED;
   }
}

static UI_ERR GSPreFunction(void *parm)
{
   if (GetValveType(*(U_INT8 * const)parm) == GAS_SAMPLING)
   {
      return OK;
   }
   else
   {
      return NOT_INSTALLED;
   }
}

static UI_ERR MPPreFunction(void *parm)
{
   if( GetValveType(*(U_INT8 * const)parm) == MULTIPOSITION )
   {
      return OK;
   }
   else
   {
      return NOT_INSTALLED;
   }
}
